﻿using Helper.Models;
using System;
using Helper;
using System.Collections.ObjectModel;
using System.Windows.Documents;
using System.Windows.Input;
using System.ComponentModel;

namespace Starehe.ViewModels
{
    public class FeesStatementVM: ViewModelBase, IDataErrorInfo
    {
        FeesStatementModel statement;
        FixedDocument fd;
        public FeesStatementVM()
        {
            InitVars();
            CreateCommands();
        }

        protected override void InitVars()
        {
            
            Title = "FEES STATEMENT";
            statement = new FeesStatementModel();
        }

        protected override void CreateCommands()
        {            
            GenerateStatementCommand = new RelayCommand(async o =>
            {
                FeesStatementModel ts = await
                     DataAccess.GetFeesStatementAsync(statement.StudentID, statement.StartDate, statement.EndDate);
                statement.Transactions = ts.Transactions;
                PrintHelper p = new PrintHelper(statement);
                Document = p.FixedDocSeq;
            }, o =>
            {
               return (statement.EndDate >statement.StartDate)
                   &&(statement.StudentID>0)&&DataAccess.StudentExists(statement.StudentID);                  
            });
        }
       
        public int SelectedStudentID
        {
            get { return statement.StudentID; }

            set
            {
                if (value != statement.StudentID)
                {
                    statement.StudentID = value;
                    NotifyPropertyChanged("SelectedStudentID");
                }
            }
        }

        public FeesStatementModel Statement
        {
            get { return statement; }

            set
            {
                if (value != statement)
                {
                    statement = value;
                    NotifyPropertyChanged("Statement");
                }
            }
        }

        public FixedDocument Document
        {
            get { return this.fd; }

            private set
            {
                if (value != this.fd)
                {
                    this.fd = value;
                    NotifyPropertyChanged("Document");
                }
            }
        }
                
        public string Error
        {
            get { return null; }
        }
        
        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "SelectedStudentID":
                        {
                            if (this.SelectedStudentID<=0)
                                return "Required value";
                            else if (!DataAccess.StudentExists(this.SelectedStudentID))
                                return "Student does not exist";
                            else
                                return null;
                        }
                }
                return null;
            }
        }

        public ICommand GenerateStatementCommand
        {
            get;
            private set;
        }

        public override void Reset()
        {
            SelectedStudentID = 0;
            Statement = new FeesStatementModel();
            Document = null;
        }
    }
}
